package com.jade.explorer.ui;

import java.io.File;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.dirscan.FileSystem;
import com.jade.explorer.views.JadeSharedImages;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class CloneDialog extends Dialog {

	private String _oldPath;
	private String _newPath;
	private Text text;
	private String _temp;
	private String _ext;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public CloneDialog(Shell parentShell,String path, String ext) {
		super(parentShell);
		_oldPath = path;
		_ext = ext;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);

		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(11, 13, 64, 64);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		
		Label lblEnterNewFile = new Label(container, SWT.NONE);
		lblEnterNewFile.setBounds(10, 78, 141, 15);
		lblEnterNewFile.setText("Enter new file name:");
		
		text = new Text(container, SWT.BORDER);
		text.setBounds(10, 99, 424, 21);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_temp = text.getText();
			}
		});
		
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("Clone file", 10, 10, true); 
				} 
		}); 
		
		return container;
	}

	
	public String getClone(){
		return _newPath;
	}
	
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Shell shell = super.getShell();
		Button Ok = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		Ok.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if(FileSystem.checkInput(_temp)){
					File f = new File(_oldPath);;
					
					_newPath = FileSystem.copyFileWithName(_oldPath, f.getParent().toString(), (_temp + _ext));
				}
				else{
					MessageDialog.openError(shell, "Error", "File name cant contain any of following characters:\n< > / \\ * : ; ? \" ' | .");
					open();
				}
					
				
			}
		});
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 246);
	}
}
